home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug231 / class.c < prev    next >
Text File  |  1987-06-17  |  5KB  |  205 lines

  1. /*
  2.     Little Smalltalk
  3.         class instance creation and deletion
  4.  
  5.         timothy a. budd  10/84
  6. */
  7. /*
  8.     The source code for the Little Smalltalk System may be freely
  9.     copied provided that the source of all files is acknowledged
  10.     and that this condition is copied with each file.
  11.  
  12.     The Little Smalltalk System is distributed without responsibility
  13.     for the performance of the program and without any guarantee of
  14.     maintenance.
  15.  
  16.     All questions concerning Little Smalltalk should be addressed to:
  17.  
  18.         Professor Tim Budd
  19.         Department of Computer Science
  20.         Oregon State University
  21.         Corvallis, Oregon
  22.         97331
  23.         USA
  24. */
  25. # include <stdio.h>
  26. # include "object.h"
  27. # include "file.h"
  28. # include "number.h"
  29. # include "symbol.h"
  30. # include "string.h"
  31. # include "primitive.h"
  32. # define streq(x,y) (strcmp(x,y) == 0)
  33.  
  34. extern class *Array, *ArrayedCollection;
  35.  
  36. extern object *o_object, *o_empty, *o_number, *o_magnitude;
  37. extern object *o_smalltalk, *o_acollection;
  38.  
  39. static mstruct *fr_class = 0;
  40. int ca_class = 0;    /* count class allocations */
  41.  
  42. # define CLASSINITMAX 30
  43.  
  44. static class cl_table[CLASSINITMAX];
  45.  
  46. class_init()
  47. {    class *p;
  48.     mstruct *new;
  49.     int i;
  50.  
  51.     for (p = cl_table, i = 0; i < CLASSINITMAX; i++, p++) {
  52.         new = (mstruct *) p;
  53.         new->mlink = fr_class;
  54.         fr_class = new;
  55.         }
  56. }
  57.  
  58. class *new_class()
  59. {    class *new;
  60.  
  61.     if (fr_class) {
  62.         new = (class *) fr_class;
  63.         fr_class = fr_class->mlink;
  64.         }
  65.     else {
  66.         new = structalloc(class);
  67.         ca_class++;
  68.         }
  69.  
  70.     new->c_ref_count = 0;
  71.     new->c_size = CLASSSIZE;
  72.     sassign(new->file_name, o_nil);
  73.     sassign(new->class_name, o_nil);
  74.     new->super_class = (object *) 0;
  75.     sassign(new->c_inst_vars, o_nil);
  76.     new->context_size = 0;
  77.     sassign(new->message_names, o_nil);
  78.     sassign(new->methods, o_nil);
  79.     return(new);
  80. }
  81.  
  82. class *mk_class(classname, args)
  83. char *classname;
  84. object **args;
  85. {    class *new;
  86.     object *new_iarray();
  87.  
  88.     new = new_class();
  89.     assign(new->class_name, args[0]);
  90.     if (! streq(classname, "Object"))
  91.         sassign(new->super_class, args[1]);
  92.     assign(new->file_name, args[2]);
  93.     assign(new->c_inst_vars, args[3]);
  94.     assign(new->message_names, args[4]);
  95.     assign(new->methods, args[5]);
  96.     new->context_size = int_value(args[6]);
  97.     new->stack_max = int_value(args[7]);
  98.  
  99.     if (streq(classname, "Array")) {
  100.         assign(Array, new);
  101.         assign(o_empty, new_iarray(0));
  102.         }
  103.     else if (streq(classname, "ArrayedCollection")) {
  104.         assign(ArrayedCollection, new);
  105.         assign(o_acollection, new_inst(new));
  106.         assign(o_empty, new_iarray(0));
  107.         }
  108.     else if (streq(classname, "False"))
  109.         assign(o_false, new_inst(new))
  110.     else if (streq(classname, "Magnitude"))
  111.         assign(o_magnitude, new_inst(new))
  112.     else if (streq(classname, "Number"))
  113.         assign(o_number, new_inst(new))
  114.     else if (streq(classname, "Object"))
  115.         assign(o_object, new_inst(new))
  116.     else if (streq(classname, "Smalltalk"))
  117.         assign(o_smalltalk, new_inst(new))
  118.     else if (streq(classname, "True"))
  119.         assign(o_true, new_inst(new))
  120.     else if (streq(classname, "UndefinedObject"))
  121.         assign(o_nil, new_inst(new))
  122.     return(new);
  123. }
  124.  
  125. /* new_sinst - new instance with explicit super object */
  126. object *new_sinst(aclass, super)
  127. class *aclass;
  128. object *super;
  129. {    object *new;
  130.     char *classname, buffer[80];
  131.  
  132.     if (! is_class(aclass))
  133.         cant_happen(4);
  134.     classname = symbol_value(aclass->class_name);
  135.     if (    streq(classname, "Block") ||
  136.         streq(classname, "Char") ||
  137.         streq(classname, "Class") ||
  138.         streq(classname, "Float") ||
  139.         streq(classname, "Integer") ||
  140.         streq(classname, "Process") ||
  141.         streq(classname, "Symbol") ) {
  142.         sprintf(buffer,"%s: does not respond to new", classname);
  143.         sassign(new, new_str(buffer));
  144.         primitive(ERRPRINT, 1, &new);
  145.         obj_dec(new);
  146.         if (super) /* get rid of unwanted object */
  147.             {obj_inc((object *) super);
  148.              obj_dec((object *) super);}
  149.         new = o_nil;
  150.         }
  151.     else if (streq(classname, "File")) {
  152.         new = new_file();
  153.         if (super) /* get rid of unwanted object */
  154.             {obj_inc((object *) super);
  155.              obj_dec((object *) super);}
  156.         }
  157.     else if (streq(classname, "String")) {
  158.         new = new_str("");
  159.         if (super)
  160.             assign(((string *) new)->s_super_obj, super);
  161.         }
  162.     else {
  163.         new = new_obj(aclass, (aclass->c_inst_vars)->size, 1);
  164.         if (super)
  165.             sassign(new->super_obj, super);
  166.         }
  167.     return(new);
  168. }
  169.  
  170. object *new_inst(aclass)
  171. class *aclass;
  172. {    object *super, *sp_class_name, *lookup_class();
  173.     class *super_class;
  174.  
  175.     if (! is_class(aclass))
  176.         cant_happen(4);
  177.     if (aclass == o_object->class)
  178.         return(o_object);
  179.     super = (object *) 0;
  180.     sp_class_name = aclass->super_class;
  181.     if (sp_class_name && is_symbol(sp_class_name)) {
  182.         super_class = (class *)
  183.             lookup_class(symbol_value(sp_class_name));
  184.         if (super_class && is_class(super_class))
  185.             super = new_inst(super_class);
  186.         }
  187.     return(new_sinst(aclass, super));
  188. }
  189.  
  190. free_class(c)
  191. class *c;
  192. {
  193.     if (! is_class(c))
  194.         cant_happen(8);
  195.     obj_dec(c->class_name);
  196.     if (c->super_class)
  197.         obj_dec((object *) c->super_class);
  198.     obj_dec(c->file_name);
  199.     obj_dec(c->c_inst_vars);
  200.     obj_dec(c->message_names);
  201.     obj_dec(c->methods);
  202.     ((mstruct *) c )->mlink = fr_class;
  203.     fr_class = (mstruct *) c;
  204. }
  205.